package com.hsyt.pms.controller.system;

import com.alibaba.dubbo.config.annotation.Reference;
import com.hsyt.pfc.support.rpc.PageResult;
import com.hsyt.pfc.support.util.EnumToDataUtil;
import com.hsyt.pfc.support.util.LoggerUtil;
import com.hsyt.pms.api.common.PltAuth;
import com.hsyt.pms.api.common.PltLog;
import com.hsyt.pms.api.common.ResultMessage;
import com.hsyt.pms.api.dto.system.SysStreetDto;
import com.hsyt.pms.api.enums.AbleStatus;
import com.hsyt.pms.api.vo.system.SysStreetVo;
import com.hsyt.pms.controller.base.BaseController;
import com.hsyt.pms.sys.dubbo.SysCityService;
import com.hsyt.pms.sys.dubbo.SysStreetService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.MessageFormat;

/**
 * @ClassName: SysStreetController
 * @Description: 街道服务控制层
 * @author: wangze@hsyuntai.com
 * @date: 2019/1/29 18:19
 */
@RestController
@RequestMapping("/pms/system/street")
public class SysStreetController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SysStreetController.class);

    @Reference(group = "pmsSystem", registry = "pms", version = "0.0.1")
    private SysStreetService sysStreetService;

    @Reference(group = "pmsSystem", registry = "pms", version = "0.0.1")
    private SysCityService sysCityService;

    @RequestMapping(value = "/page")
    @PltAuth(auth = "ROLE_STREET_MG")
    public ModelAndView page(ModelMap model) {
        String statusComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(AbleStatus.class);
        model.put("statusComboData", statusComboData);
        return new ModelAndView("city/pageStreet");
    }

    @RequestMapping(value = "/editStreet")
    @PltAuth(auth = "ROLE_STREET_MG_ADD,ROLE_STREET_MG_MODIFY")
    public ModelAndView editStreet(ModelMap model, @RequestParam("type") String type) {
        String statusComboData = EnumToDataUtil.getDHEnumsValuesFromEnum(AbleStatus.class);
        model.put("statusComboData", statusComboData);
        model.put("type", type);
        return new ModelAndView("system/win/editStreet");
    }

    @RequestMapping(value = "/toEditForm")
    @PltAuth(auth = "ROLE_STREET_MG_ADD,ROLE_STREET_MG_MODIFY")
    public ModelAndView toEditForm(ModelMap model, @RequestParam("formType") String formType) {
        model.put("formType", formType);
        return new ModelAndView("city/formStreet");
    }

    @RequestMapping(value = "/findPageData")
    @PltAuth(auth = "ROLE_STREET_MG")
    public @ResponseBody
    PageResult findPageData(@RequestBody SysStreetDto dto) {
        PageResult<SysStreetVo> pageResult = new PageResult<SysStreetVo>();
        try {
            pageResult = sysStreetService.searchPageDataByDto(dto);
        } catch (Exception e) {
            pageResult.setResult(false);
            String message = MessageFormat.format("SysStreetController.searchPageData error: {0}", e.getMessage());
            pageResult.setMessage(message);
            LoggerUtil.error(LOGGER, message);
        }
        return pageResult;
    }


    @PltLog(log = "保存街道")
    @RequestMapping(value = "/save")
    @PltAuth(auth = "ROLE_STREET_MG_ADD,ROLE_STREET_MG_MODIFY")
    public @ResponseBody
    ResultMessage save(@RequestBody SysStreetDto dto, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "成功");
        try {
            if (dto.getId() == null) {
                if (sysStreetService.count(dto) > 0) {
                    result.change(false, "街道已存在!");
                    return result;
                }
                if (StringUtils.isBlank(dto.getStatus())) {
                    dto.setStatus(AbleStatus.ENABLED.getCode());
                }
                sysStreetService.addStreet(dto);
            } else {
                sysStreetService.updateByPrimaryKeySelective(dto);
            }
        } catch (Exception e) {
            result.change(false, "保存失败 , 错误信息:" + e.getMessage());
            LoggerUtil.error(LOGGER, "SysStreetController-save error: {0}", e.getMessage());
        }
        return result;
    }

    @PltLog(log = "删除街道")
    @PltAuth(auth = "ROLE_STREET_MG_DELETE")
    @RequestMapping(value = "/deleteStreet")
    public @ResponseBody
    ResultMessage deleteStreet(@RequestBody SysStreetDto dto, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "删除成功");
        if (dto.getId() == null) {
            result.change(false, "参数中街道id为空");
            return result;
        }
        dto.setStatus(AbleStatus.DISABLED.getCode());
        try {
            sysStreetService.updateByPrimaryKeySelective(dto);
        } catch (Exception e) {
            result.change(false, "删除失败,错误信息:" + e.getMessage());
            LoggerUtil.error(LOGGER, "SysStreetController deleteStreet error: {0}", e.getMessage());
        }
        return result;
    }

    @PltLog(log = "启用/停用街道")
    @RequestMapping(value = "/doUndo")
    @PltAuth(auth = "ROLE_STREET_MG_DELETE")
    public @ResponseBody
    ResultMessage doUndo(@RequestBody SysStreetDto dto, HttpServletRequest request) {
        ResultMessage result = new ResultMessage(true, "删除成功");
        if (dto.getId() == null) {
            result.change(false, "参数中街道id为空");
            return result;
        }
        try {
            sysStreetService.updateByPrimaryKeySelective(dto);
        } catch (Exception e) {
            result.change(false, "删除失败,错误信息:" + e.getMessage());
            LoggerUtil.error(LOGGER, "SysStreetController deleteStreet error: {0}", e.getMessage());
        }
        return result;
    }
}
